package hsl.study.controller;


import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.messages.AssistantMessage;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.LinkedList;
import java.util.List;

/**
 * @Description:
 * @Author: hsl
 * @CreateDate: 2025/7/31 17:46
 * @Version: 1.0
 */


@RestController
@RequestMapping("/assistant")
public class AssistantController {

    @Resource(name = "qwenPlusChatClient")
    private ChatClient qwenClient;

    // 保存历史信息
    List<Message> messageList = new LinkedList<>();

    /**
     *
     *
     * @param question
     * @return http://localhost:7007/assistant/chat1?question=北京故宫
     */
    @GetMapping("/chat1")
    public String chat1(String question) {
        // 问题
        messageList.add(new UserMessage(question));

        // 答案
        AssistantMessage assistantMessage = qwenClient.prompt()
                //关键实现代码 ChatClient.messages(List<Message>)
                .messages(messageList)
                .call()
                .chatResponse()
                .getResult()
                .getOutput();

        messageList.add(assistantMessage);
        String result = assistantMessage.getText();
        System.out.println("result = " + result);
        return result;
    }
}
